package com.ruoyi.web.controller.ccc3gc;

import com.ccc3gc.mysystem.bean.Authentication;
import com.ccc3gc.mysystem.bean.Staff;
import com.ccc3gc.mysystem.bean.User;
import com.ccc3gc.mysystem.service.AuthenticationService;
import com.ccc3gc.mysystem.service.StaffService;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.config.ServerConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDate;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/api/authentication")
public class AuthenticationController extends BaseController {
    private static final String UPLOAD_AUTH_IDIMAGE="/authentication/identityImage";
    private static final String UPLOAD_AUTH_STIMAGE="/authentication/studentIdImage";
    @Autowired
    public AuthenticationService authenticationService;
    @Autowired
    StaffService staffService;
    @Autowired
    private ServerConfig serverConfig;
    //认证通过
    @PostMapping("/adopt")
    public AjaxResult adoptAuth(@RequestBody Authentication authentication){
        authentication.setState(1);
        int rows = authenticationService.updateAuthentication(authentication);
        if (rows>0) {
            Staff staff = new Staff();
            staff.setAuthId(authentication.getAuthenticationId());
            staff.setStaffName(authentication.getName());
            String code = UUID.randomUUID().toString().replaceAll("[^0-9]","").substring(0, 6);
            staff.setJobNumber(LocalDate.now().toString().replace("-","")+code);
            staff.setPassword("123456");
            staffService.instertStaff(staff);
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }
    //认证信息列表
    @GetMapping("/list")
    public TableDataInfo list(Authentication authentication){
        startPage();
        List<Authentication> list = authenticationService.selectAuthenticationList(authentication);
        return getDataTable(list);
    }

    //根据Id获取认证
    @GetMapping("/queryAuth/{authId}")
    public AjaxResult queryAuthentication(@PathVariable int authId){
        Authentication authentication = authenticationService.queryAuthentication(authId);
        return AjaxResult.success(authentication);
    }

    //根据认证Id查询配送员
    @GetMapping("/staffByAuthId/{authId}")
    public AjaxResult staffByAuthId(@PathVariable int authId){
        Staff staff = new Staff();
        staff.setAuthId(authId);
        List<Staff> staffList = staffService.selectStaffList(staff);
        int rows = staffList.size();
        AjaxResult ajax = AjaxResult.success();
        ajax.put("rows",rows);
        if (rows != 0) {
            ajax.put("staff",staffList.get(0).getStaffId());
        }
        return ajax;
    }

    //添加认证信息
    @PostMapping("/addAuthentication")
    public AjaxResult addAuthentication(@RequestBody Authentication authentication){
        authentication.setStudentIdImage("profile/upload/authentication/studentIdImage/ccc.png");
        authentication.setIdentityImage("profile/upload/authentication/identityImage/111.jpg");
        authentication.setState(0);
        int rows = authenticationService.instertAuthentication(authentication);
        if (rows>0){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    //修改认证信息
    @PostMapping("/updateAuthentication")
    public AjaxResult updateAuthentication(@RequestBody Authentication authentication){
        int rows = authenticationService.updateAuthentication(authentication);
        if(rows>0){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    //修改状态
    @GetMapping("/updateState/{authId}")
    public AjaxResult updateState(@PathVariable int authId){
        Authentication authentication=new Authentication();
        authentication.setState(1);
        authentication.setAuthenticationId(authId);
        int rows = authenticationService.updateAuthentication(authentication);
        if (rows>0){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    //根据Id删除认证信息
    @DeleteMapping("/{authenticationId}")
    public AjaxResult deleteAuthentication(@PathVariable int authenticationId){
        int rows = authenticationService.deleteAuthenticationById(authenticationId);
        if (rows>0){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    //批量删除
    @DeleteMapping("/deleteAuthenticationBatch/{authenticationIds}")
    public AjaxResult deleteAuthenticationBatch(@PathVariable List<Integer> authenticationIds){
        int rows = authenticationService.deleteAuthenticationByIds(authenticationIds);
        if (rows>0){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    //上传图片
    @PostMapping("/upload")
    public AjaxResult uploadImage(@RequestBody MultipartFile file,int image) throws Exception
    {
        String path = RuoYiConfig.getUploadPath();
        String uploadPath="";
        if (image == 1) {
            uploadPath=path+UPLOAD_AUTH_IDIMAGE;
        }else {
            uploadPath=path+UPLOAD_AUTH_STIMAGE;
        }
        String fileName = FileUploadUtils.upload(uploadPath, file);
        String url = serverConfig.getUrl() + fileName;
        AjaxResult ajax = AjaxResult.success();
        ajax.put("imgUrl", url);
        ajax.put("fileUrl", fileName);
        ajax.put("fileUuid", FileUtils.getName(fileName));//存的文件名
        ajax.put("fileName", file.getOriginalFilename());//原始文件名
        ajax.put("image",image);
        return ajax;
    }
}
